$(function(){
    var temp=$("<div><img src='res/image/system/ajaxload.gif' alt='' /></div>");
    $("#panel").append(temp);
    var roleId=$("#roleId").val();
    $.ajax({
        url: 'system/role!queryPermission.action?systemRole.id='+roleId+'&__t='+new Date(),
        type: 'GET',
        dataType: 'json',
        error: function(){
            temp.remove();
            $("#panel").html("<div class='text-center' style='margin:10px;'><h4>网络错误，请重试</h4><div>");
        },
        success: function(data){
            temp.remove();
            if(!data||data.length==0){
                $("#panel").html("<div class='text-center' style='margin:10px;'><h4>无任何权限</h4><div>");
            }else{
                var d=data;
                d=doDataConvert(d)
                $.fn.zTree.init($("#tree"),{
                    view: {
                        showIcon: false,
                        nameIsHTML: true,
                        showTitle:false
                    }
                },d);
            }
        }
    });
});

function doDataConvert(obj){
    for(var i=0;i<obj.length;i++){
        var name=obj[i].name;
        var icon=obj[i].icon?obj[i].icon:'fa fa-circle-o';
        obj[i].name='<span class="'+icon+'" />&nbsp;'+name;//+'<button class="btn btn-link btn-xs"></button>';
        if(obj[i].children&&obj[i].children.length>0){
            doDataConvert(obj[i].children);
        }
    }
    return obj;
}

function makeConfig(){
	$('form').submit();
}